package com.itheima.day09.Zuoye.ZuoYe6;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ZuoYe6 {
    public static void main(String[] args) {
        //Map<String,Double> world =new TreeMap<>((k1,k2)->Double.compare(world.get(k1),world.get(k2)));
        Map<String, Double> world = new HashMap<>();
        world.put("肖申克救赎", 9.7);
        world.put("霸王别姬", 9.6);
        world.put("阿甘正传", 9.6);
        world.put("辛德勒的名单", 9.6);
        world.put("美丽人生", 9.5);
        world.put("千与千寻", 9.4);
        world.put("泰坦尼克号", 9.4);
        world.put("这个杀手不太冷", 9.4);
        world.put("忠犬八公的故事", 9.4);
        world.put("盗梦空间", 9.3);
        Map<String, Double> china = new HashMap<>();
        china.put("阳光普照", 8.5);
        china.put("气球", 7.9);
        china.put("拆弹专家", 7.9);
        china.put("一秒钟", 7.8);
        china.put("送你一朵小红花", 7.8);
        china.put("金都", 7.7);
        china.put("叔叔", 7.7);
        china.put("八佰", 7.6);
        china.put("夺冠", 7.3);
        china.put("我和我的家乡", 7.3);
        //错误的写法：Map <String,Double>world2 =world.entrySet()
        // .stream()
        // .sorted((o1, o2) -> Double.compare(o2.getValue(),o1.getValue()))
        // .collect(Collectors.toMap( Map.Entry::getKey,
        //                        Map.Entry::getValue,));
        Map<String, Double> sortedWorld = world.entrySet()
                .stream()
                .sorted((o1, o2) -> Double.compare(o2.getValue(),o1.getValue()))
                //compareingByValue()是指通过值进行排序，reversed()是指倒序，如果不写reversed()则默认升序
                .collect(Collectors.toMap(
                        x ->x.getKey(),
                        y ->y.getValue(),
                        //以上代码是将键值对映射成新的键值对
                        (oldValue, newValue) -> oldValue, // 如果有重复键，保留旧值
                        LinkedHashMap::new // 保证排序后的顺序
                ));
        System.out.println(sortedWorld);
        Map<String, Double> sortedChina = china.entrySet()
                .stream()
                .sorted((Map.Entry.<String, Double>comparingByValue().reversed()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue,
                        LinkedHashMap::new
                ));
                System.out.println(sortedChina);
        System.out.println("--------------------------------");
        //需求1：打印全球影片排行榜中的前三甲影片名
        sortedWorld.entrySet().stream().limit(3).forEach(entry-> System.out.println(entry.getKey()+"---"+entry.getValue()));
        System.out.println("--------------------------------");
        //需求2：打印华人影片排行榜中倒数5名的影片名
        sortedChina.entrySet().stream().skip(sortedChina.size()-5).forEach(entry-> System.out.println(entry.getKey()+"---"+entry.getValue()));
        System.out.println("--------------------------------");
        //需求3：将两个排行榜中的前5名挑出来共同存入新的集合
        Map<String, Double> newworld = sortedWorld.entrySet().stream().limit(5).collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
        Map<String, Double> newchina =sortedChina.entrySet().stream().limit(5).collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
        Stream <Map.Entry<String, Double>>newpaiming = Stream.concat(newworld.entrySet().stream(),newchina.entrySet().stream());
        Map<String, Double> newpm = newpaiming.sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,LinkedHashMap::new));
        System.out.println(newpm);
        System.out.println("--------------------------");

        //需求4：定义电影Film类(属性：电影名，评分)，将上面新集合中的数据，封装到Film对象中。
        List<Film> list = newpm.entrySet().stream().map(entry -> new Film(entry.getKey(), entry.getValue())).collect(Collectors.toList());
        System.out.println(list);
//        List<Film> filmList = newpaiming
//                .map(entry -> new Film((String) ((Map.Entry) entry).getKey(), (Double) ((Map.Entry) entry).getValue()))
//                .collect(Collectors.toList());


    }
}
